<?php
namespace Application\Model;

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
use Zend\Db\ResultSet\ResultSet;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Adapter\AdapterInterface;

class CategoryTable
{

    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function getCategorybyId($id)
    {
        $id = (int) $id;
        $rowset = $this->tableGateway->select(array(
            'id' => $id
        ));
        $row = $rowset->current();
        if (! $row) {
            throw new \Exception("Could not find row option $id");
        }
        return $row;
    }

    public function getTree()
    {
        $sql = "select id,parent_id,name, concat(ifnull(parent_id,id),'#',id) path from  category order by path";
        /* @var $adapter \Zend\Db\Adapter\AdapterInterface */
        $adapter = $this->tableGateway->getAdapter();
        $statement =$adapter->query($sql);
        return $statement->execute();
    }

    public function fetchAll($paginated = false)
    {
        if ($paginated) {
            // create a new Select object for the table album
            $select = new Select('category');

            // create a new result set based on the Album entity
            $resultSetPrototype = new ResultSet();
            $resultSetPrototype->setArrayObjectPrototype(new Category());
            // create a new pagination adapter object
            $paginatorAdapter = new DbSelect(
                // our configured select object
                $select,
                // the adapter to run it against
                $this->tableGateway->getAdapter(),
                // the result set to hydrate
                $resultSetPrototype);
            $paginator = new Paginator($paginatorAdapter);
            return $paginator;
        }
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }

    public function getMenu($paginated = false)
    {

        // create a new Select object for the table album
        $select = new Select('category');
        $select = $select->where(array(
            new \Zend\Db\Sql\Predicate\IsNull('parent_id')
        ));
        // create a new result set based on the Album entity
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new Category());
        // create a new pagination adapter object
        $paginatorAdapter = new DbSelect(
            // our configured select object
            $select,
            // the adapter to run it against
            $this->tableGateway->getAdapter(),
            // the result set to hydrate
            $resultSetPrototype);
        $paginator = new Paginator($paginatorAdapter);
        return $paginator;
    }

    public function getGroupProduct($parentID, $paginated = false)
    {

        // create a new Select object for the table album
        $select = new Select('category');
        $select = $select->where(array(
            "parent_id=$parentID",
            new \Zend\Db\Sql\Predicate\IsNotNull('parent_id')
        ));
        // create a new result set based on the Album entity
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new Category());
        // create a new pagination adapter object
        $paginatorAdapter = new DbSelect(
            // our configured select object
            $select,
            // the adapter to run it against
            $this->tableGateway->getAdapter(),
            // the result set to hydrate
            $resultSetPrototype);
        $paginator = new Paginator($paginatorAdapter);
        return $paginator;
    }

    public function saveCategory(Category $category)
    {
        $data = array(
            'parent_id' => $category->parent_id,
            'name' => $category->name,
            'desc' => $category->desc
        );

        $id = (int) $category->id;

        if ($id == 0) {
            $this->tableGateway->insert($data);
        } else {
            if ($this->getCategorybyId($id)) {
                $this->tableGateway->update($data, array(
                    'id' => $id
                ));
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
    }

    public function deleteCategory($id)
    {
        $this->tableGateway->delete(array(
            'id' => $id
        ));
    }
}